//音色引擎初始化 (shupu-player.js)
// 音频上下文初始化
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
const gainNode = audioCtx.createGain();
gainNode.gain.value = 0.5;
gainNode.connect(audioCtx.destination);

// 乐器音色配置
const instruments = {
    guqin: {
        type: "sine",
        envelope: { attack: 0.01, decay: 0.3, sustain: 0.2, release: 0.5 }
    },
    xiao: {
        type: "triangle",
        envelope: { attack: 0.1, decay: 0.5, sustain: 0.4, release: 1.0 }
    }
};

// 新谱符号-音高映射
const pitchMap = {
    "⿱仲上": 392.00,  // g1
    "⿱林尺": 440.00,  // a1
    "⿱黄合": 293.66,  // d1
    "⿱太四": 329.63,  // e1
    "⿱南工": 493.88,  // b1
    "⿱姑一": 369.99,  // f#1
    "⿱应凡": 554.37,  // c#2
    "⿱无下凡": 523.25, // c2
    "⿱蕤勾": 415.30,  // g#1
    "⿱夹下一": 311.13  // d#1
};
// 全曲乐谱数据（简化版）
const score = [
    { lyric: "明月几时有", symbols: ["⿱仲上", "⿱林尺", "⿱黄合", "⿱太四", "⿱南工", "⿱林尺"] },
    { lyric: "把酒问青天", symbols: ["⿱姑一", "⿱应凡", "⿱无下凡", "⿱蕤勾", "⿱姑一", "⿱仲上"] },
    // ...完整乐谱数据
// 添加律制选项
const temperaments = {
    "三分损益": { "⿱蕤勾": 415.30 },
    "纯律": { "⿱蕤勾": 407.75 }
};
];

// 播放引擎
let currentNote = 0;
let isPlaying = false;
let tempo = 60;

function playNote(pitch, duration) {
    const osc = audioCtx.createOscillator();
    const gain = audioCtx.createGain();
    
    osc.type = instruments.guqin.type;
    osc.frequency.value = pitch;
    
    // 包络控制
    const now = audioCtx.currentTime;
    gain.gain.setValueAtTime(0, now);
    gain.gain.linearRampToValueAtTime(1, now + instruments.guqin.envelope.attack);
    gain.gain.exponentialRampToValueAtTime(
        instruments.guqin.envelope.sustain, 
        now + instruments.guqin.envelope.attack + instruments.guqin.envelope.decay
    );
    gain.gain.exponentialRampToValueAtTime(0.001, now + duration - instruments.guqin.envelope.release);
    
    osc.connect(gain).connect(gainNode);
    osc.start();
    osc.stop(now + duration);
}

function playScore() {
    if (currentNote >= score.flatMap(s => s.symbols).length) {
        isPlaying = false;
        return;
    }
    
    // 获取当前音符
    const flatSymbols = score.flatMap(s => s.symbols);
    const currentSymbol = flatSymbols[currentNote];
    const duration = 60 / tempo; // 转换为秒
    
    // 高亮当前音符
    document.querySelectorAll('.shupu-symbol').forEach(el => el.classList.remove('current-note'));
    document.querySelectorAll('.shupu-symbol')[currentNote].classList.add('current-note');
	/ 在JS中添加：
	
	});
    
    // 播放音频
    playNote(pitchMap[currentSymbol], duration * 0.8); // 留空拍尾
    
    currentNote++;
    setTimeout(playScore, duration * 1000);
}

// 控制事件绑定
document.getElementById('play-btn').addEventListener('click', () => {
    if (isPlaying) {
        isPlaying = false;
        currentNote = 0;
        document.getElementById('play-btn').textContent = '播放';
    } else {
        isPlaying = true;
        document.getElementById('play-btn').textContent = '停止';
        playScore();
    }
});

document.getElementById('instrument-select').addEventListener('change', (e) => {
   	 currentInstrument = e.target.value;
    document.getElementById('tempo-value').textContent = `${tempo} BPM`;
document.getElementById('temperament-select').addEventListener('change', (e) => {
    Object.assign(pitchMap, temperaments[e.target.value]);
});

// 渲染乐谱到页面
function renderScore() {
    const container = document.getElementById('score-container');
    
    score.forEach(line => {
        const lineDiv = document.createElement('div');
        lineDiv.style.margin = '15px 0';
        
        // 添加歌词
        const lyricSpan = document.createElement('span');
        lyricSpan.textContent = line.lyric + ' ';
        lyricSpan.style.marginRight = '10px';
        lineDiv.appendChild(lyricSpan);
        
        // 添加新谱符号
        line.symbols.forEach(symbol => {
            const symbolSpan = document.createElement('span');
            symbolSpan.className = 'shupu-symbol';
            symbolSpan.textContent = symbol;
            lineDiv.appendChild(symbolSpan);
        });
        
        container.appendChild(lineDiv);
    });
}

// 初始化
document.addEventListener('DOMContentLoaded', renderScore);

